Hey there. I am trying to use a regular expression to find and replace '_' characters with '-', but I only want to replace those underscores if that text string is within curly braces. For instance, say I have the following: |
Re: Problem with using regular expression to find and replace I don't think it is possible to do what you want with the DOORS search and replace function. Although you can search for '_' inside the curly braces with regular expression \{.*_.*\}, you can not replace the '_' this way, as replace will always replace the complete expression and not only the '_'s. - Michael |
Re: Problem with using regular expression to find and replace MichaelGeorg - Fri Dec 14 03:17:09 EST 2012 A simple and straight-forward DXL routine could lool like this:
string myReplace(string sInput){
int i
char c
Buffer B
string sReturnValue
bool bBetweenBrackets
B = create
B = ""
bBetweenBrackets = false
for i in 0:length sInput -1 do {
c = sInput[i]
if (c == '{'){bBetweenBrackets = true}
else if (c == '}'){bBetweenBrackets = false}
B += bBetweenBrackets ? (c == '_' ? '-' : c) : c
}
sReturnValue = stringOf B
delete B
return sReturnValue
}
string s = "This_is_a_sentence_with_underscores_{but_only_want_to_replace_these}_and_no_others."
print myReplace(s)
It does not check for specific cases, like sentences with only one opening bracket, but it is a start. |
Re: Problem with using regular expression to find and replace Albert's solution seems adequate. If you wanted a Regexp solution, I suppose it would be outlined like this:
Regexp reBracedText = regexp2(something that finds text between braces "{" and "}")
// match 1: Any number of characters that does not include '{'; ends with '{'
// match 2: Any number of characters up to be excluding the first '}' character
// match 3: Any number of characters that starts with '}'
RemainingText = OriginalText
bufResults = ""
while (!null RemainingText)
{ if (!reBracedText RemainingText)
{ bufResults += RemainingText
RemainingText = ""
}
else
{ bufResults += RemainingText[match 1] // part up to including the first brace
bufResults += ReplaceUnderscoresToDashes(RemainingText[match 2])
RemainingText = RemainingText[match 3]
}
}
AdjustedtText = stringOf(bufResults)
|